package com.example.newspublish.service;

import com.example.newspublish.mapper.UserMapper;
import com.example.newspublish.pojo.JWTUserDetails;
import com.example.newspublish.pojo.entity.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
@Slf4j
@Service
@RequiredArgsConstructor
public class JWTUserDetailsService implements UserDetailsService {
    @Autowired
    UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("经过SysUserDetailsService.loadUserByUsername=====");
        User userAuth = userMapper.getUserAuth(username);
        log.info("userAuthInfo1=====>{}",userAuth);
        if (userAuth == null) {
            throw new UsernameNotFoundException(username);
        }
        return new JWTUserDetails(userAuth.getId(), userAuth.getUsername()) {
        };
    }
}
